<template>
  <div class="app-container">

    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="商品类别">
        <el-select v-model="searchObj.goodsType" placeholder="选择商品类别">
          <el-option label="全部" value=""></el-option>
          <el-option label="零食" value="1"></el-option>
          <el-option label="地方特产" value="2"></el-option>
          <el-option label="旅游纪念品" value="3"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-input v-model="searchObj.goodsId" placeholder="商品id"/>
      </el-form-item>

      <el-form-item>
        <el-input v-model="searchObj.goodsName" placeholder="商品名称"/>
      </el-form-item>

        <el-form-item label="状态">
          <el-select v-model="searchObj.goodsStatus" placeholder="选择状态">
            <el-option label="全部" value=""></el-option>
            <el-option label="未上架" value="0"></el-option>
            <el-option label="已上架" value="1"></el-option>
          </el-select>
        </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
        <el-button type="default" @click="resetData()">清空</el-button>
        <!--  导出  -->
        <el-button type="primary"><a href="http://localhost:9000/zukeApi/product/goods/exportData" style="text-decoration: none;color: white" target="_blank"> 导出商品表</a></el-button>

      </el-form-item>

    </el-form>

    <!-- banner列表 -->
    <el-table v-loading="listLoading" :data="list"
              border
              fit
              highlight-current-row>

      <el-table-column
        label="序号"
        width="60"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column label="商品logo">
        <template slot-scope="scope">
          <img
            :src="'http://localhost:9005/' + scope.row.goodsImage"
            width="80"/>
        </template>
      </el-table-column>

      <el-table-column prop="goodsName" label="商品名称"/>
      <el-table-column prop="goodsPrice" label="商品价格"/>
      <el-table-column prop="goodsDescript" label="商品详情"/>
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          <el-tag effect="plain" v-text="scope.row.goodsStatus === 0 ? '未上线' : '已上线'"></el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="添加时间"/>

      <el-table-column label="操作" width="230" align="center">
        <template slot-scope="scope">
          <el-button type="warning" size="mini" @click="handleGoodsEdit(scope.row.goodsId)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleGoodsDelete(scope.row.goodsId,scope.row.goodsName)">删除</el-button>
          <el-button v-if="scope.row.goodsStatus === 1" type="primary" size="mini" @click="updateStatus(scope.row.goodsId,0)">下架</el-button>
          <el-button v-if="scope.row.goodsStatus === 0" type="danger" size="mini" @click="updateStatus(scope.row.goodsId,1)">上架</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="fetchData"
      @size-change="changeSize"
    />

<!--  编辑按钮弹出层  -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">

      <el-form ref="goods" :model="goods" label-width="80px" >
        <el-form-item label="商品名称">
          <el-input v-model="goods.goodsName"></el-input>
        </el-form-item>

        <el-form-item label="商品价格">
          <el-input v-model="goods.goodsPrice"></el-input>
        </el-form-item>

        <el-form-item label="商品类型">
          <el-select v-model="goods.goodsType" placeholder="请选择商品类型">
            <el-option label="零食" value="1"></el-option>
            <el-option label="地方特产" value="2"></el-option>
            <el-option label="旅游纪念品" value="3"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品logo" prop="introduction">
          <el-upload
            ref="edit"
            action="http://localhost:9000/zukeApi/product/goods/updateGoods"
            :auto-upload="false"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            list-type="picture"
            :file-list="fileList"
            :data="goods"
            :limit="1"
            :before-upload="beforeAvatarUpload"
            name="filedata"
            multiple
          >
            <el-button size="mini" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>

        <el-form-item label="商品描述">
          <el-input type="textarea" v-model="goods.goodsDescript"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="closeEditGoods()">取 消</el-button>
      <el-button type="primary" @click="EditGoods()">确 定</el-button>
    </span>

    </el-dialog>

  </div>
</template>


<script>
import axios from 'axios'

export default {
  name: 'list',
  data(){
    return{
      formData: new FormData(),
      goods: {},         // 编辑页面数据
      fileList: [
        {
          name: "",
          url: ""
        },
      ],
      dialogVisible: false,
      listLoading: true, // 数据是否正在加载
      list: null, // banner列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 5, // 每页记录数
      searchObj: {
        goodsId: null,
        goodsName: null,
        goodsType: null,
        goodsStatus: null
      }, // 查询表单对象
    }
  },
  created() {
    // 调用商品列表方法
    this.fetchData(1)
  },
  methods: {

    handleRemove(file, fileList) {
      // console.log(file, fileList);
    },
    handlePreview(file) {
      // console.log(file);
      this.fileList[0].url = file.url
    },
    //调用的是表单中的:before-upload="beforeAvatarUpload"
    beforeAvatarUpload(file) {
      this.formData.append('filedata',file)
      return false;
    },
    // 商品列表
    fetchData(page = 1) {
      this.page = page
      this.$http({
        url: this.$http.adornUrl(`/product/goods/list/${page}/${this.limit}`),
        method: 'get',
        params: this.searchObj
      }).then(({data}) => {
        this.list = data.data.records
        this.total = data.data.total
        this.listLoading = false
      })

    },
    // 页码切换
    changeSize(val) {
      this.limit = val
      this.page = 1
      this.fetchData(1)
    },
    // 重置查询表单
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },
    handleGoodsDelete(id, goodsName) {
      this.$confirm(`确定对  ${goodsName}  删除？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/product/goods/deleteById'),
          method: 'post',
          data: this.$http.adornData(id, false)
        }).then(({data}) => {
          if (data && data.code === 200) {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.fetchData(this.page)
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {
      })
    },
    // 更新商品状态
    updateStatus(id, status) {
      this.$http({
        url: this.$http.adornUrl(`/product/goods/updateGoodsStatus/${id}/${status}`),
        method: 'get'
      }).then(() => {
        this.fetchData(this.page)
      })
    },
    // 打开商品更新弹出层
    handleGoodsEdit(goodsId) {
      this.$http({
        url: this.$http.adornUrl(`/product/goods/findById/${goodsId}`),
        method: 'get'
      }).then((res) => {
        this.goods = res.data.data
        this.goods.goodsType = this.goods.goodsType.toString()
        this.dialogVisible = true
        this.fileList[0].name =res.data.data.goodsImage
        this.fileList[0].url = 'http://localhost:9005/' + res.data.data.goodsImage
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.goods = {}
          done();
        })
        .catch(_ => {
        });
    },
    closeEditGoods() {
      this.dialogVisible = false
      this.goods = {}
    },
    // 提交图片和表单请求
    EditGoods() {
      this.formData.append('goods',JSON.stringify(this.goods))
        this.$refs.edit.submit()
        axios({
          method: 'post',
          url: 'http://localhost:9000/zukeApi/product/goods/updateGoods',
          headers: {
            'Content-Type': 'multipart/form-data'
          },
          data: this.formData
        }).then(({data}) => {
          if (data && data.code === 200) {
            this.$message({
              message: '更新成功',
              type: 'success',
              duration: 1500
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      this.goods = {}
      this.dialogVisible = false
    },

  }
}
</script>
